//
// Created by binny on 2019/9/26.
//

#include "dy_register_jni.h"
#include <stdio.h>
#include <string.h>

void hello(JNIEnv *env, jobject jb) {
    printf("动态注册hello");
}

jstring getPassword(JNIEnv *env, jobject jb, jstring js) {
    char *c_str = (*env)->GetStringUTFChars(env, js, JNI_FALSE);
//    char password[6] = "123456";
    char password[7] = "123456";
//    char *password = "123456中国";
    strcat(c_str, password);
    //转码
    jstring new_js = (*env)->NewStringUTF(env, c_str);
    printf(" new_js= %s", new_js);
    printf(" new_js= %d", sizeof(new_js));
    return new_js;
}


//需要动态注册的方法数组
static const JNINativeMethod mMethods[] = {
        {"hello",       "()V",                                    (void *) hello},
        {"getPasswords", "(Ljava/lang/String;)Ljava/lang/String;", (jstring *) getPassword}
};
//需要动态注册native方法的类名
static const char *mClassName = "com/binny/jni/HelloWorld";

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env = NULL;
//获得 JniEnv
    int r = (*vm)->GetEnv(vm, (void **) &(env), JNI_VERSION_1_4);
    if (r != JNI_OK) {
        return -1;
    }
    jclass mainActivityCls = (*env)->FindClass(env, mClassName); // 注册 如果小于0则注册失败
    r = (*env)->RegisterNatives(env, mainActivityCls, mMethods, 2);
    if (r != JNI_OK) {
        return -1;
    }
    return JNI_VERSION_1_4;
}